home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MSG Demo 1.4.source Folder / Demo ƒ / Shell ƒ / progress.c < prev    next >
Text File  |  1994-04-23  |  7KB  |  209 lines

  1. /**********************************************************************\
  2.  
  3. File:        progress.c
  4.  
  5. Purpose:    This module handles the progress bar and dealing with
  6.             events while the progress bar is up.
  7.             
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "Power.h"
  26. #include "program globals.h"
  27. #include "progress.h"
  28. #include "dialogs.h"
  29. #include "environment.h"
  30. #include "menus.h"
  31. #include "main.h"
  32.  
  33. enum
  34. {
  35.     progressDialogID = 210,
  36.     progressText = 1,
  37.     progressBar = 2
  38. };
  39.  
  40. DialogPtr        gProgressDlog=0L;        /* pointer to progress dialog */
  41. int                gOldForegroundWaitTime;
  42.  
  43. /*-----------------------------------------------------------------------------------*/
  44. /* internal stuff for progress.c                                                     */
  45.  
  46. static    Rect            box;                /* box of actual progress bar in dialog */
  47. static    unsigned long    curProgress;        /* current progress value */
  48. static    unsigned long    maxProgress;        /* maximum progress value */
  49.  
  50. static pascal void DrawProgressBar(WindowPtr theWindow, int item);
  51.  
  52.  
  53. static pascal void DrawProgressBar(WindowPtr theWindow, int item)
  54. /* the useritem procedure for the actual progress bar in the dialog */
  55. {
  56.     Rect                tempBox;
  57.     unsigned long        length;
  58.     unsigned long        width;
  59.     long double            temp;
  60.     
  61.     SetPort(theWindow);        /* the dialog */
  62.     FrameRect(&box);        /* progress area outline */
  63.     
  64.     length = box.right - box.left;
  65.     width = length * curProgress;
  66.     if ((width / length) != curProgress)    /* if we have overflow problems... */
  67.     {                                        /* use long double math instead */
  68.         temp = ((long double)curProgress) / ((long double)maxProgress);
  69.         width = temp * length;
  70.     }
  71.     else width /= maxProgress;
  72.     
  73.     tempBox = box;
  74.     InsetRect(&tempBox, 1, 1);
  75.     tempBox.left += width;
  76.     FillRect(&tempBox, ltGray);        /* gray background in progress area */
  77.     
  78.     tempBox = box;
  79.     InsetRect(&tempBox, 1, 1);
  80.     tempBox.right = tempBox.left + width - 1;
  81.     ForeColor(cyanColor);
  82.     PaintRect(&tempBox);            /* paint progress area as much as we've progressed */
  83.     ForeColor(blackColor);            /* important!  always set ForeColor back to black */
  84. }
  85.  
  86. DialogPtr OpenProgressDialog(unsigned long max, Str255 theTitle)
  87. {
  88.     int                itemType;
  89.     Handle            itemH;
  90.     Rect            otherBox;
  91.     
  92.     PositionDialog('DLOG', progressDialogID);    /* see dialogs.c */
  93.     /* get the progress dialog from .rsrc file */
  94.     gProgressDlog = GetNewDialog(progressDialogID, 0L, (WindowPtr)-1L);
  95.     if (gProgressDlog == 0L)
  96.         return 0L;
  97.     
  98.     /* set up useritem procedure to draw the progress area (see above) */
  99.     GetDItem(gProgressDlog, progressBar, &itemType, &itemH, &box);
  100.     SetDItem(gProgressDlog, progressBar, userItem + itemDisable, DrawProgressBar, &box);
  101.     
  102.     curProgress = 0;        /* start at empty */
  103.     maxProgress = max;        /* max value as passed in parameter */
  104.     
  105.     SetWTitle((WindowPtr)gProgressDlog, theTitle);    /* set title as passed in parameter */
  106.     
  107.     ShowWindow(gProgressDlog);    /* show it */
  108.     DrawDialog(gProgressDlog);    /* draw it */
  109.     
  110.     UpdateProgressDialog(0);    /* draw progress area as empty (zero progress) */
  111.     
  112.     gOldForegroundWaitTime=gForegroundWaitTime;
  113.     gForegroundWaitTime=0;
  114.     
  115.     gInProgress=TRUE;            /* so we know progress bar is up */
  116.     AdjustMenus();                /* dims almost everything */
  117.     DrawMenuBar();                /* needed so menus look dimmed immediately */
  118.     
  119.     return gProgressDlog;
  120. }
  121.  
  122. void SetProgressText(Str255 p1, Str255 p2, Str255 p3, Str255 p4)
  123. {
  124.     Str255            totalStr;
  125.     unsigned char    i;
  126.     int                itemType;
  127.     Handle            itemH;
  128.     Rect            otherBox;
  129.     
  130.     /* DON'T use ParamText to set text in progress dialog.  ParamText handles are
  131.        low-mem globals and can be changed if you switch out of the application and
  132.        another program displays an alert/dialog with ParamText strings.  Instead,
  133.        add up all four strings into one big string (max 255 characters total) and
  134.        sets the dialog item to be that text. */
  135.     totalStr[0]=0x00;
  136.     for (i=1; i<=p1[0]; i++)
  137.         totalStr[++totalStr[0]]=p1[i];
  138.     for (i=1; i<=p2[0]; i++)
  139.         totalStr[++totalStr[0]]=p2[i];
  140.     for (i=1; i<=p3[0]; i++)
  141.         totalStr[++totalStr[0]]=p3[i];
  142.     for (i=1; i<=p4[0]; i++)
  143.         totalStr[++totalStr[0]]=p4[i];
  144.     GetDItem(gProgressDlog, 1, &itemType, &itemH, &otherBox);
  145.     SetIText((ControlHandle)itemH, totalStr);
  146. }
  147.  
  148. void UpdateProgressDialog(unsigned long cur)
  149. {
  150.     curProgress = cur;        /* set our global variable of current progress */
  151.     if (curProgress >= maxProgress)        /* can't be >= than max progress */
  152.         curProgress = maxProgress-1;
  153.     
  154.     SetPort(gProgressDlog);
  155.     
  156.     DrawProgressBar(gProgressDlog, progressBar);    /* draw progress area manually */
  157.     
  158.     if (gHasPowerManager)    /* so Powerbooks won't go down to 1 MHz during a */
  159.         IdleUpdate();        /* lengthy progress operation */
  160. }
  161.  
  162. void DismissProgressDialog(void)
  163. {
  164.     if (gProgressDlog!=0L)        /* so you can be sloppy and dismiss it even if it's */
  165.         DisposDialog(gProgressDlog);    /* not up (: */
  166.     gProgressDlog=0L;            /* so we know it's gone */
  167.     gInProgress=FALSE;            /* not in progress anymore */
  168.     gForegroundWaitTime=gOldForegroundWaitTime;
  169.     AdjustMenus();                /* so adjust menus accordingly */
  170.     DrawMenuBar();                /* and redraw menubar to see effect immediately */
  171. }
  172.  
  173. #define TheCancelKey    '.'
  174.  
  175. Boolean DealWithOtherPeople(void)
  176. {
  177.     /* this is just a small useful function to see if the user has cancelled */
  178.     /* a lengthy operation with command-period; could come in handy, I suppose, */
  179.     /* in a somewhat bizarre set of circumstances... */
  180.     /* Note that this procedure will break under AUX */
  181.     /* Note also that this returns TRUE if there has been no attempt to cancel */
  182.     
  183.     Boolean            foundEvent;
  184.     EvQElPtr        eventQPtr;
  185.     QHdrPtr            eventQHdr;
  186.     char            thisChar;
  187.     long            isCmdKey;
  188.     
  189.     foundEvent=FALSE;
  190.     eventQHdr=GetEvQHdr();
  191.     eventQPtr=(EvQElPtr)(eventQHdr->qHead);
  192.     while ((eventQPtr!=0L) && (!foundEvent))
  193.     {
  194.         if (eventQPtr->evtQWhat==keyDown)
  195.         {
  196.             thisChar=(char)((eventQPtr->evtQMessage)&charCodeMask);
  197.             isCmdKey=(eventQPtr->evtQModifiers)&cmdKey;
  198.             if (isCmdKey!=0L)
  199.                 foundEvent=(thisChar==TheCancelKey);
  200.         }
  201.         if (!foundEvent)
  202.             eventQPtr=(EvQElPtr)(eventQPtr->qLink);
  203.     }
  204.  
  205.     while (HandleSingleEvent());
  206.     
  207.     return !foundEvent;
  208. }
  209.